#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libmemcached/memcached.h>

void touch(char *filename)
{
    int i, j, count;
    FILE *file;
    char server[1024], key[33], *sName;
    unsigned long block_size, pad;
    memcached_st *memc;
    memcached_return_t rc;

    file = fopen(filename, "r");
    if(file == NULL){
        perror(filename);
        exit(0);
    }

    fscanf(file, "%lu %lu", &block_size, &pad);

    count = 0;
    for(i = 0; i < 6; i++){
        fscanf(file, "%s %s", server, key);
        sName = (char *) malloc(sizeof(char) * (strlen(server) + 10));
        sprintf(sName, "--SERVER=%s", server);
        memc = memcached(sName, strlen(sName));
        if(memc != NULL){
             rc = memcached_touch(memc, key, strlen(key), (time_t) 0);
             if(strcmp(memcached_strerror(memc, rc), "SUCCESS") != 0) count++;
        }
        memcached_free(memc);
        free(sName);
    }
    if(count > 2) fprintf(stderr, "Only %d of 6 blocks are still on the servers. The file will be unable to be retrieved.\n", 6 - count);
    fclose(file);
}
